import service from '../require/request';

/**
 * 简历查询参数接口
 */
export interface ResumeSearchParams {
    /**
     * 用户ID
     */
    uid: string;

    /**
     * 学生ID
     */
    sid: string;

    /**
     * 记录ID
     */
    record_id: string;
}

/**
 * 简历信息接口
 */
export interface ResumeInfo {
    /**
     * 简历ID
     */
    resume_id: string;

    /**
     * 简历名称
     */
    resume_name: string;
}

/**
 * 简历查询响应接口
 */
export interface ResumeSearchResponse {
    /**
     * 状态码
     */
    code: number;

    /**
     * 消息
     */
    msg: string;

    /**
     * 简历列表
     */
    data: ResumeInfo[];
}

/**
 * 简历上传参数接口
 */
export interface ResumeUploadParams {
    /**
     * 用户ID
     */
    uid: string;

    /**
     * 学生ID
     */
    sid: string;

    /**
     * 记录ID
     */
    record_id: string;

    /**
     * 文件
     */
    file: File;
}

/**
 * 简历上传响应接口
 */
export interface ResumeUploadResponse {
    /**
     * 简历ID
     */
    resume_id: string;

    /**
     * 简历名称
     */
    resume_name: string;
}

/**
 * 简历删除参数接口
 */
export interface ResumeDeleteParams {
    /**
     * 用户ID
     */
    uid: string;

    /**
     * 学生ID
     */
    sid: string;

    /**
     * 记录ID
     */
    record_id: string;

    /**
     * 简历ID
     */
    resume_id: string;
}

/**
 * 获取简历列表
 * @param params 查询参数
 * @returns Promise<ResumeSearchResponse>
 */
export const getResumeList = async (params: ResumeSearchParams): Promise<ResumeInfo[]> => {
    try {
        const response = await service.post<ResumeInfo[], ResumeInfo[]>(
            '/b/sme/cdc/career/detail/resume_info/search',
            params
        );

        return response;
    } catch (error) {
        console.error('获取简历列表失败:', error);
        throw error;
    }
};

/**
 * 上传简历
 * @param params 上传参数
 * @returns Promise<ResumeUploadResponse>
 */
export const uploadResume = async (params: ResumeUploadParams): Promise<ResumeUploadResponse> => {
    try {
        const formData = new FormData();
        formData.append('uid', params.uid);
        formData.append('sid', params.sid);
        formData.append('record_id', params.record_id);
        formData.append('file', params.file);

        const response = await service.post<ResumeUploadResponse, ResumeUploadResponse>(
            '/b/sme/cdc/career/detail/resume_info/upload',
            formData,
            {
                headers: {
                    'Content-Type': 'multipart/form-data'
                    // 'Content-Type': 'application/json'
                }
            }
        );

        return response;
    } catch (error) {
        console.error('上传简历失败:', error);
        throw error;
    }
};

/**
 * 删除简历
 * @param params 删除参数
 * @returns Promise<any>
 */
export const deleteResume = async (params: ResumeDeleteParams): Promise<any> => {
    try {
        const response = await service.post<any, any>('/b/sme/cdc/career/detail/resume_info/delete', params);

        return response;
    } catch (error) {
        console.error('删除简历失败:', error);
        throw error;
    }
};

/**
 * 下载简历
 * @param resumeId 简历ID
 * @param resumeName 简历名称
 * @returns void
 */
export const downloadResume = (resumeId: string, resumeName: string): void => {
    try {
        const url = `/b/sme/cdc/career/detail/resume_info/download?resume_id=${resumeId}`;

        // 创建一个隐藏的a标签
        const a = document.createElement('a');
        a.style.display = 'none';
        a.href = url;
        a.download = resumeName;

        // 添加到body并触发点击
        document.body.appendChild(a);
        a.click();

        // 移除a标签
        document.body.removeChild(a);
    } catch (error) {
        console.error('下载简历失败:', error);
        throw error;
    }
};
